package anonymouschat.client.frames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import anonymouschat.messages.TextMessage;

/**
 * One on one chat frame, only supply two people to chat. ownerUser is the local
 * client, while targetUser is the user owner is chatting with.
 * 
 * @author Winters
 */
public class OneOnOneChatFrame extends JFrame
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private HashMap<String, ArrayList<String>> tempChatLog;

    ObjectOutputStream out;

    private String ownerUserID;

    private String targetUserID;

    private JTextArea textArea;

    private JTextArea msgArea;

    private JScrollPane textScr;

    private JScrollPane msgScr;

    private JButton sendButton;

    final int WIDTH = 400;

    final int HEIGHT = 500;

    /**
     * Constructor by two user names and chatLog, outStream
     * 
     * @param owner
     * @param target
     * @param tempChatLog
     * @param out
     */
    public OneOnOneChatFrame( String owner, String target,
            HashMap<String, ArrayList<String>> tempChatLog,
            ObjectOutputStream out ) {
        this.out = out;
        this.tempChatLog = tempChatLog;
        ownerUserID = owner;
        targetUserID = target;
        setTitle( "Chatting with " + targetUserID );
        setSize( WIDTH, HEIGHT );
        // setResizable( false );
        setVisible( true );
        getContentPane().setLayout( null );

        // textArea
        textArea = new JTextArea();
        textScr = new JScrollPane( textArea );
        textScr.setSize( WIDTH, HEIGHT - 100 );
        textScr.setLocation( 0, 0 );

        // sendMsg
        msgArea = new JTextArea();
        msgScr = new JScrollPane( msgArea );
        msgScr.setLocation( 10, HEIGHT - 90 );
        msgScr.setSize( WIDTH - 130, 50 );

        // sendButton
        sendButton = new JButton( "Send" );
        sendButton.setLocation( WIDTH - 120, HEIGHT - 90 );
        sendButton.setSize( 100, 50 );
        sendButton.addActionListener( new ActionListener()
        {

            @Override
            public void actionPerformed( ActionEvent e ) {
                // TODO Auto-generated method stub
                sendTextMsg( msgArea.getText() );
                msgArea.setText( "" );
            }

        } );

        this.getContentPane().add( textScr );
        this.getContentPane().add( msgScr );
        this.getContentPane().add( sendButton );

        this.validate();
    }

    private void sendTextMsg( String text ) {
        // TODO Auto-generated method stub
        TextMessage msg = new TextMessage();
        msg.srcUser = ownerUserID;
        {
            msg.destinationUser = targetUserID;
            String tempData = "You ->" + msg.destinationUser + ": " + text
                    + "\n";
            textArea.append( tempData );
            if( tempChatLog.containsKey( targetUserID ) ) {
                tempChatLog.get( targetUserID ).add( tempData );
            } else {
                tempChatLog.put( targetUserID, new ArrayList<String>() );
                tempChatLog.get( targetUserID ).add( tempData );
            }
        }
        msg.encryptedData = text;
       // try {
       //     ChatFrame.sendMessage( text );
      //  } catch( IOException e1 ) {
      //      textArea.setText( "Error occured when sending TextMessage object to server" );
      //      e1.printStackTrace();
      //  }
    }
}
